﻿@model IEnumerable<eduscrum.ViewModels.SprintVM>
@using eduscrum.ViewModels
@using System.Linq

<div style="float:left;">
    <h2>Sprints</h2>
    <select id="sprintlist" size="20" onchange="selectSprint(this);">
        <option value="!add_sprint!" style="color:#339933;" selected="selected">Add Sprint</option>
    </select>
</div>

@{
    var vd = new ViewDataDictionary(this.ViewData);
    ProjectVM projectVM = (ProjectVM)TempData["Project"];
    Html.RenderPartial("~/Views/Sprint/Create.cshtml", new SprintVM { StartDate = DateTime.Now, EndDate = DateTime.Now, Project = projectVM }, vd);
    Html.RenderPartial("~/Views/Sprint/Edit.cshtml", new SprintVM { Project = (ProjectVM)ViewData["Project"] }, vd);
}

<div id="addUserStories" style="float:left;padding-top: 50px;">
    <button onclick="addUserStories()"><<</button>
    <br />
    <button onclick="removeUserStories()">>></button>
</div>
<h2 onclick="alert(document.forms['editSprint']['sprintbackloglistedit'].options[1].value);">Unassigned User Stories</h2>
<select id="unassignedbackloglist" size="20" multiple="multiple">
</select>

<script type="text/javascript">

    document.getElementById("tbl_editSprint").style.display = "none";
    var mode = "create";
    var unassignedUserStories = new Array();
    var sprints = new Array();
    var sprintList = document.getElementById("sprintlist");
    var unassignedBackloglist = document.getElementById("unassignedbackloglist");
    var option;
    var sprint;
    var userstory;
    @{
        SprintVM sprint;
        List<UserStoryVM> sprintbacklog = new List<UserStoryVM>();
        UserStoryVM userstory;
    }
    @for (int i = 0; i < Model.Count();i++ )
    {
        sprint = Model.ElementAt(i);
        sprintbacklog = sprint.Project.Backlog.Where(u => u.Sprint != null && u.Sprint.SprintID == sprint.SprintID).ToList();
        @:sprint = new Sprint(@sprint.SprintID, "@sprint.StartDate.ToString("dd-MM-yyyy")", "@sprint.EndDate.ToString("dd-MM-yyyy")", new Array());
        for(int j = 0;j < sprintbacklog.Count();j++)
        {
            userstory = sprintbacklog[j];
            @:sprint.backlog.push(new UserStory(@userstory.UserStoryID, "@userstory.Title", "@userstory.Description", @userstory.UserStoryID));
        }
        @:sprints.push(sprint);
        @:option = document.createElement("option");
        @:option.setAttribute("value", "@sprint.SprintID");
        @:option.innerHTML = "Sprint @(i+1)";
        @:sprintList.appendChild(option);
    }
    
    @{ List<UserStoryVM> backlog = ViewBag.Backlog;}

    @foreach (var item in backlog.Where(u => u.Sprint == null).OrderByDescending(u => u.Priority))
    {
        @:us = new UserStory(@item.UserStoryID,"@item.Title", "@item.Description", @item.StoryPoints, @item.Priority);
        @:unassignedUserStories.push(us);
    }

    resetUnassignedBackloglist();
    
</script>
